﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Omnia.DataModels;
using Omnia.Enums;

namespace Omnia.ViewModels
{
    public class CaseViewModel : MainViewModel
    {
        #region Properties
        private bool fliterCurrentYear = false;
        public bool FliterCurrentYear
        {
            get { return fliterCurrentYear; }
            set
            {
                fliterCurrentYear = value;
                RaisePropertyChanged(() => FliterCurrentYear);
                RaisePropertyChanged(() => ShowCasesCollection);
            }
        }

        public ObservableCollection<CaseInfo> ShowCasesCollection
        {
            get
            {
                var items = FliterCurrentYear ? CasesCollection.Where(t => t.StartDate > new DateTime(DateTime.Now.Year, 1, 1)) : CasesCollection;
                if (items.Count() > 0)
                {
                    foreach (CaseInfo item in items)
                    {
                        IEnumerable<TransactionInfo> transactions = Database.CollectionOfTransactions.Where(t => t.CaseGuid == item.Guid);
                        item.SumOfIncome = transactions.Where(t => t.Kind == TransactionKindValue.Income).Sum(t => t.Amount);
                        item.SumOfExpense = transactions.Where(t => t.Kind == TransactionKindValue.Expense).Sum(t => t.Amount);
                        item.TransactionCount = transactions.Count();
                    }
                }
                return new ObservableCollection<CaseInfo>(items.OrderByDescending(c => c.StartDate));
            }
        }
        #endregion
    }
}
